package com.wecan.oauth.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wecan.api.saasdata.RoleData;
import com.wecan.base.DTO.base.KeyDTO;
import com.wecan.oauth.DTO.role.AddRoleDTO;
import com.wecan.oauth.DTO.role.SelectRolePageDTO;
import com.wecan.oauth.DTO.security.UpdateRoleDTO;
import com.wecan.base.PO.Role;
import com.wecan.base.controller.BaseController;
import com.wecan.base.model.Result;
import com.wecan.base.utils.AppNumberUtils;
import com.wecan.cache.annotation.AccessLimit;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * @Author liuzhihao
 * @Date 2022.01.20
 * @Version ${version}
 * @Description ${description}
 */
@RestController
@RequestMapping(value = "/role")
@Tag(name = "角色接口")
@Validated
public class RoleController extends BaseController {

    @Autowired
    private RoleData roleData;

    @Operation(summary = "分页获取角色", description = "获取角色",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(mediaType = "application/json")),
            responses = {@ApiResponse(description = "角色列表", content = @Content(mediaType = "application/json"))})
    @Parameters({
            @Parameter(name = "selectRolePageDTO", required = true, description = "查询角色参数", schema = @Schema(implementation = SelectRolePageDTO.class))
    })
    @AccessLimit
    @PostMapping(value = "/listpage")
    public Result<Page<Role>> getRoleByPage(@RequestBody @Valid SelectRolePageDTO selectRolePageDTO) {
        Role role = new Role();
        role.setName(selectRolePageDTO.getName());
        Page<Role> roles = roleData.selectPage(selectRolePageDTO.getPageNum(), selectRolePageDTO.getPageSize(), role);
        return Result.content(roles);
    }

    @Operation(summary = "新增角色", description = "新增角色",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(mediaType = "application/json")),
            responses = {@ApiResponse(description = "新增角色", content = @Content(mediaType = "application/json"))})
    @Parameters({
            @Parameter(name = "addRoleDTO", required = true, description = "新增角色参数", schema = @Schema(implementation = AddRoleDTO.class))
    })
    @AccessLimit
    @PostMapping(value = "/add")
    public Result addRole(@RequestBody @Valid AddRoleDTO addRoleDTO) {
        Role role = new Role();
        role.setName(addRoleDTO.getName());
        role.setPermissionIds(addRoleDTO.getPermissionIds());
        role.setRemark(addRoleDTO.getRemark());
        role.setStatus(addRoleDTO.getStatus());
        if (roleData.selectByName(role.getName()) != null) {
            return Result.failure("角色名称存在");
        }
        int rows = roleData.insert(role);
        return rows > 0 ? Result.success("新增角色成功") : Result.failure("新增角色失败");
    }


    @Operation(summary = "更新角色", description = "更新角色",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(mediaType = "application/json")),
            responses = {@ApiResponse(description = "更新角色", content = @Content(mediaType = "application/json"))})
    @Parameters({
            @Parameter(name = "addRoleDTO", required = true, description = "新增角色参数", schema = @Schema(implementation = AddRoleDTO.class))
    })
    @AccessLimit
    @PostMapping(value = "/update")
    public Result updateRole(@RequestBody @Valid UpdateRoleDTO updateRoleDTO) {
        Role result = roleData.selectByName(updateRoleDTO.getName());
        if (result != null && result.getId() != updateRoleDTO.getId()) {
            return Result.failure("角色名称已存在");
        }
        Role role = new Role();
        role.setId(updateRoleDTO.getId());
        role.setName(updateRoleDTO.getName());
        role.setStatus(updateRoleDTO.getStatus());
        role.setRemark(updateRoleDTO.getRemark());
        role.setPermissionIds(updateRoleDTO.getPermissionIds());
        int rows = roleData.updateById(role);
        return rows > 0 ? Result.success("更新角色成功") : Result.failure("更新角色失败");
    }

    @Operation(summary = "删除角色", description = "删除角色",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(mediaType = "application/json")),
            responses = {@ApiResponse(description = "删除角色", content = @Content(mediaType = "application/json"))})
    @Parameters({
            @Parameter(name = "keyDTO", required = true, description = "新增角色参数", schema = @Schema(implementation = KeyDTO.class))
    })
    @AccessLimit
    @PostMapping(value = "/delete")
    public Result deleteRole(@RequestBody @Valid KeyDTO keyDTO) {
        Integer id = AppNumberUtils.toInt(keyDTO.getId());
        int rows = roleData.deleteById(id);
        return rows > 0 ? Result.success("删除角色成功") : Result.failure("删除角色失败");
    }

    @Operation(summary = "角色列表", description = "角色列表",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(mediaType = "application/json")),
            responses = {@ApiResponse(description = "角色列表", content = @Content(mediaType = "application/json"))})
    @AccessLimit
    @PostMapping(value = "/listall")
    public Result<List<Role>> getRoleList() {
        List<Role> roles = roleData.selectList();
        return Result.content(roles);
    }

    @Operation(summary = "查询角色", description = "查询角色",
            requestBody = @io.swagger.v3.oas.annotations.parameters.RequestBody(content = @Content(mediaType = "application/json")),
            responses = {@ApiResponse(description = "查询角色", content = @Content(mediaType = "application/json"))})
    @Parameters({
            @Parameter(name = "keyDTO", required = true, description = "查询角色", schema = @Schema(implementation = KeyDTO.class))
    })
    @AccessLimit
    @PostMapping(value = "/queryrole")
    public Result<Role> queryRoleById(@RequestBody @Valid KeyDTO keyDTO) {
        Integer id = AppNumberUtils.toInt(keyDTO.getId());
        Role role = roleData.queryRoleById(id);
        return Result.content(role);
    }
}
